package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private SpecGroupService specGroupService;
    @Autowired
    private SpecParamService specParamService;
    @GetMapping("/groups/of/category")
    /**todo:
     * 根据分类id查询组集合
     */
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategoryId(@RequestParam("id") Integer cid){
        return ResponseEntity.ok(specGroupService.queryGroupByCategoryId(cid));
    }
    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> queryParamByGroupId(@RequestParam(value = "groupId",required = false) Integer gid,
                                                                  @RequestParam(value = "categoryId",required = false)Integer cid,
                                                                  @RequestParam(value = "searching",required = false) Boolean searching){
        return ResponseEntity.ok(specParamService.queryParams(gid,cid,searching));
    }

    /**todo：
     * 新增规格组
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.save(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**todo:
     * 修改规格组
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.updateById(specGroupDTO.toEntity(SpecGroup.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**todo:
     * 新增规格参数
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.save(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO specParamDTO){
        specParamService.updateById(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @return 规格组集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecList(@RequestParam("id") Long categoryId) {
        return ResponseEntity.ok(specGroupService.querySpecList(categoryId));
    }

    /**
     * 根据参数id删除
     * @param id
     * @return
     */
    @DeleteMapping("/param/{id}")
    public ResponseEntity<Void> updateParam(@PathVariable("id") Integer id){
        specParamService.removeById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
 }
